package com.example.twotwo_project;


import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.annotation.SuppressLint;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.app.ProgressDialog;


@SuppressLint("ValidFragment")
public class Frag02Activity extends Fragment { // 기존 Fragment 를 FragmentActivity로 변경
	Context mContext;
	
	final int MENU_back=Menu.FIRST;
	final int MENU_forward=Menu.FIRST+1;
	
	private String key1="50347c1299b65992f671f6428ff53084";
	private ProgressDialog dialog;
	
	private ListView myList;
	private NaverParser Nparser;
	private CustomAdapter2 adapter;
	
	ArrayList<LocalData> data;
	private String info;
	
	final int count=30;
	int start=1;
	

	public Frag02Activity(Context context) {
		mContext = context;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.serach_locallist, null);
		
		info = "헬스";
		
		myList = (ListView)view.findViewById(R.id.listView); // view.을 붙여야됨
		Nparser = new NaverParser(key1);
		
		getNewList(info, count, start);
		/*ActivityGroup activityGroup = new ActivityGroup();
		LocalActivityManager localActivityManager = activityGroup.getLocalActivityManager();
		Intent intent = new Intent(mContext, MapView.class);
		Window window = localActivityManager.startActivity("hosted", intent);
		View view = window.getDecorView();*/


		return view;
	}

	private final Handler handler = new Handler(){

		@Override
		public void handleMessage(final Message msg) {
			//dialog.dismiss();
			adapter = new CustomAdapter2(mContext, R.layout.serach_itemlist, data); // adapter 생성
			myList.setAdapter(adapter); // NaverLocal.this --> mContext

			info();
		}
	};
	
	
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

		menu.add(0, MENU_back, 0, "이전페이지로");
		menu.add(0, MENU_forward, 1, "다음페이지로");
		
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch(item.getItemId()){
		case MENU_back:{
			if(start <= 30){
				Toast.makeText(mContext, "이전 페이지가 없습니다", Toast.LENGTH_SHORT).show();
			}else{
				start -= 30;
				getNewList(info, count, start);
			}
			break;
		}
		case MENU_forward:{
			start += 30;
			getNewList(info, count, start);
			break;
		}
		}
		
		return super.onOptionsItemSelected(item);
	}

	public void getNewList(final String inform, final int count, final int starts){
	//	dialog = ProgressDialog.show(mContext, "Loading..", "지역 정보를 불러옵니다", true, false);
		new Thread(){
			public void run(){
				data = Nparser.getBookData2(inform, count, starts); // 네이버 api를 이용해 정보를 얻어옴
				handler.sendEmptyMessage(0);
			}
		}.start();
	}
	
	public void info(){
		myList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) { // getApplication -- > getActivity
				Intent intent = new Intent(mContext, Healthinfo.class);
				 
				/*TextView tv = (TextView)findViewById(android.R.id.text2);
				intent.putExtra("id", tv.getText().toString());*/
				
				startActivity(intent);
			}
		});
	}

	

}

